﻿
using System.Collections.Generic;
using System.Data;
using System;
using SXT.Party.VoteActivity.Model;
using SXT.Party.VoteActivity.BLL;
using ZHDJ.MobileInterface.Common;
namespace ZHDJ.MobileInterface
{
    public partial class VoteActivitySubmit : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            VoteActivitySubmitOutParam result = new VoteActivitySubmitOutParam();
            VoteActivitySubmitInParam param = new VoteActivitySubmitInParam();
            string logMsg = "";
            bool isFlag = true;
            string LevelID = "";
            string currentVoteActivityID = "";

            try
            {
                param = inParam as VoteActivitySubmitInParam;


                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员!";

                    isFlag = false;
                }


                DataSet dsUser = new DataSet();
                DataSet dsVote = new DataSet();
                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户对象不能为空!";

                        isFlag = false;
                    }
                    if (isFlag)
                    {
                        dsUser = InterfaceHelper.GetMembersByFilter(" LoginID='" + param.user_id + "' ");
                        if (dsUser == null || dsUser.Tables[0].Rows.Count == 0)
                        {
                            result.code = "0";
                            result.desc = "用户不存在!";

                            isFlag = false;
                        }
                    }
                    #region 循环验证
                    if (isFlag)
                    {
                        foreach (var item in param.data)
                        {
                            if (isFlag)
                            {
                                if (string.IsNullOrEmpty(item.ItemID))
                                {
                                    result.code = "0";
                                    result.desc = "评议对象不能为空!";

                                    isFlag = false;
                                }
                                if (isFlag)
                                {
                                    dsVote = InterfaceHelper.GetVoteAndItemByID(item.ItemID, param.user_id);

                                    #region 提交的数据校验
                                    if (item.VoteResult != "0" && item.VoteResult != "1" && item.VoteResult != "2" && item.VoteResult != "3")
                                    {
                                        result.code = "0";
                                        result.desc = "评议结果只能为如下数值：0（满意）；1（基本满意）；2（不了解）；3（不满意）";

                                        isFlag = false;
                                    }
                                    else if (item.VoteResult == "3" && string.IsNullOrEmpty(item.VoteDesc))
                                    {
                                        result.code = "0";
                                        result.desc = "如果评议结果是“3.不满意”，则评议结果描述不能为空";

                                        isFlag = false;
                                    }
                                    #endregion

                                    #region 用户校验


                                    if (dsVote == null || dsVote.Tables[0].Rows.Count == 0)
                                    {
                                        result.code = "0";
                                        result.desc = "被评议对象不存在或者被禁用";

                                        isFlag = false;
                                    }
                                    else
                                    {
                                        currentVoteActivityID = dsVote.Tables[0].Rows[0]["ActivityID"].ToString();
                                    }
                                    #endregion

                                    #region 检查是否已经投票
                                    //if (isFlag)
                                    //{
                                    //    if (!string.IsNullOrEmpty(Convert.ToString(dsVote.Tables[0].Rows[0]["RecordID"])))
                                    //    {
                                    //        result.code = "0";
                                    //        result.desc = "你已经对该被评议对象进行了投票，不能重复投票";

                                    //        isFlag = false;
                                    //    }
                                    //}
                                    #endregion

                                    #region 该活动的投票方式为一人一票，你已经对其他评议对象进行了投票
                                    if (isFlag)
                                    {
                                        if (Convert.ToString(dsVote.Tables[0].Rows[0]["VoteType"]) == "OneToOne")
                                        {
                                            if (InterfaceHelper.IsHaveVote(param.user_id, currentVoteActivityID))
                                            {
                                                result.code = "0";
                                                result.desc = "该活动的投票方式为一人一票，你已经对其他评议对象进行了投票";

                                                isFlag = false;
                                            }
                                        }
                                    }
                                    #endregion

                                    #region 投票资格校验
                                    if (isFlag)
                                    {
                                        if (Convert.ToString(dsVote.Tables[0].Rows[0]["VoteUserType"]) == "PartyDeputy")
                                        {
                                            if (Convert.ToString(dsUser.Tables[0].Rows[0]["IsPartyWorker"]).ToLower() != "true")
                                            {
                                                result.code = "0";
                                                result.desc = "该机关民主评议只有党务工作者可以投票";

                                                isFlag = false;
                                            }
                                        }
                                        else if (Convert.ToString(dsUser.Tables[0].Rows[0]["runSate"]) != "0")
                                        {
                                            result.code = "0";
                                            result.desc = "该机关民主评议只有党员可以投票";

                                            isFlag = false;
                                        }
                                    }
                                    #endregion
                                }
                            }
                        }
                    }
                }

                #region 校验投票时间
                if (isFlag)
                {
                    VoteLevel level = Vote.GetCurrentLevel(currentVoteActivityID);
                    if (level.VoteIng)
                    {
                        LevelID = level.LevelID;
                    }
                    else
                    {
                        LevelID = "";
                        result.code = "0";
                        DateTime dt = DateTime.Now;

                        if (dt < level.BeginTime)
                        {
                            result.desc = "活动未开始";
                        }
                        else
                        {
                            result.desc = "活动已过期";
                        }
                        isFlag = false;
                    }
                }
                #endregion

                #region 批量提交
                if (isFlag)
                {
                    List<SqlCommandInfo> commandInfoItem = new List<SqlCommandInfo>();
                    foreach (var item in param.data)
                    {
                        var itemID = item.ItemID;
                        var content = item.VoteDesc ?? "";
                        var voteScore = item.VoteResult ?? "";
                        if (string.IsNullOrEmpty(voteScore))
                        {
                            continue;
                        }
                        SqlCommandInfo com = SXT.Party.VoteActivity.BLL.ModuleUtil.CreateVoteOneSql(param.user_id, itemID, LevelID, voteScore, content);
                        commandInfoItem.Add(com);
                    }
                    var rs = Vote.RunVote(param.user_id, currentVoteActivityID, LevelID, commandInfoItem);
                    result.code = "0";
                    result.desc = "操作成功";
                    switch (rs)
                    {
                        case "False":
                            result.desc = "数据加载出错";
                            break;
                        case "NoStart":
                            result.desc = "活动未开始";
                            break;
                        case "OutLogin":
                            result.desc = "找不到登录账号信息";
                            break;
                        case "CannotVote":
                            result.desc = "该机关民主评议只有党务工作者可以投票";
                            break;
                        case "End":
                            result.desc = "活动已结束";
                            break;
                        case "1":
                            result.code = "1";
                            result.desc = "操作成功";
                            break;

                    }
                }
                #endregion
                    #endregion


                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-VoteActivitySubmit", SerializeHelper.JsonSerializer<VoteActivitySubmitInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<VoteActivitySubmitInParam, VoteActivitySubmitOutParam>();
            else
                SafeProcessRequest<VoteActivitySubmitInParam, VoteActivitySubmitOutParam>();
        }
    }

    #region 请求参数
    public class VoteActivitySubmitInParam
    {
        /// <summary>
        ///	用户ID	 
        /// </summary>
        public string user_id
        { get; set; }

        public List<VoteData> data
        { get; set; }

    }

    public class VoteData
    {
        /// <summary>
        ///	被评议对象ID
        /// </summary>
        public string ItemID
        { get; set; }

        /// <summary>
        ///	评议结果 0.满意；1.基本满意；2.不满意；3.不了解
        /// </summary>
        public string VoteResult
        { get; set; }

        /// <summary>
        ///	评议结果描述 如果评议结果是“3.不了解”，则评议结果描述不能为空
        /// </summary>
        public string VoteDesc
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class VoteActivitySubmitOutParam
    {
        public VoteActivitySubmitOutParam()
        {
            code = "1";
            desc = "操作成功";
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }
    }
    #endregion
}